﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace Expense.DataLayer
{
    //implements business methods
    public partial class ExpenseClaim
    {

        #region methods;     

        /// <summary>
        /// Inserts an ExpenseClaimItem row.
        /// </summary>
        /// <param name="ExpenseClaimItem">An ExpenseClaimItem object.</param>
        /// 
        public ExpenseClaim Insert()
        {
            ExpenseClaim ExpenseClaimItem=this;
            using (ExpenseDataDataContext dc = new ExpenseDataDataContext())
            {
                // Insert the values. Datacontext will be attached here.
                ExpenseClaimItem.DateSubmitted = DateTime.Now;
                dc.ExpenseClaims.InsertOnSubmit(ExpenseClaimItem);
                try
                {
                    dc.SubmitChanges();
                    return ExpenseClaimItem;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        
        /// <summary>
        /// Updates an ExpenseClaimItem row.
        /// </summary>
        /// <param name="ExpenseClaimItem">A ExpenseClaimItem object.</param>
        public ExpenseClaim Update()
        {
            ExpenseClaim newExpenseClaimItem = this;
            using (ExpenseDataDataContext dc = new ExpenseDataDataContext())
            {
                try
                {
                    ExpenseClaim oldExpenseClaim =
                (from j in dc.ExpenseClaims
                 where j.ID == newExpenseClaimItem.ID
                 select j).SingleOrDefault();
                    this.Copy(oldExpenseClaim, newExpenseClaimItem);
                    oldExpenseClaim.DateModified = DateTime.Now;
                    dc.SubmitChanges();
                    return oldExpenseClaim;
                }               
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }


        private ExpenseClaim Copy( ExpenseClaim oldExpenseClaim, ExpenseClaim newExpenseClaim)
        {
            oldExpenseClaim.Amount = newExpenseClaim.Amount;
            oldExpenseClaim.Comments = newExpenseClaim.Comments;
            oldExpenseClaim.Description = newExpenseClaim.Description;
            oldExpenseClaim.IsApproved = newExpenseClaim.IsApproved;
            oldExpenseClaim.ModifiedBy = newExpenseClaim.ModifiedBy;
            oldExpenseClaim.Status = newExpenseClaim.Status;
            oldExpenseClaim.DateModified = newExpenseClaim.DateModified;

            return oldExpenseClaim;

        }


        public bool Delete(int ID)
        {
            try
            {
                using (ExpenseDataDataContext db = new ExpenseDataDataContext())
                {
                    var deletedItem =
                    (from t in db.ExpenseClaims 
                     where t.ID == ID
                     select t).SingleOrDefault();
                    if (deletedItem != null)
                    {
                        db.ExpenseClaims.DeleteOnSubmit(deletedItem);                          
                        db.SubmitChanges();
                    }
                }
                return true;

            }
            catch (Exception)
            {
                return false;
            }
        }

        public ExpenseClaim Get(int ID)
        {
            ExpenseDataDataContext db = new ExpenseDataDataContext();
            ExpenseClaim itemQuery =
       (from t in db.ExpenseClaims
        where t.ID == ID
        select t).SingleOrDefault();
            return itemQuery;
        }
        public List<ExpenseClaim> List()
        {
            ExpenseDataDataContext db = new ExpenseDataDataContext();
            var itemQuery =
       (from t in db.ExpenseClaims
        select t).ToList();
            return itemQuery;
        }


        #endregion
        
    }
}
